<template>
  <el-form ref="registerFormRef" :model="registerData" :rules="registerRules" label-width="80px" class="register-form">
    <!-- 手机号 -->
    <el-form-item prop="phone" label="手机" class="register-form-item">
      <el-input v-model="registerData.phone" type="tel" placeholder="请输入手机号码" clearable>
        <template #prefix>
          <el-icon class="el-input__icon"><Phone /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <!-- 密码 -->
    <el-form-item prop="password" label="密码" class="register-form-item">
      <el-input
        v-model="registerData.password"
        type="password"
        placeholder="请输入密码(至少6位)"
        show-password
        clearable
      >
        <template #prefix>
          <el-icon class="el-input__icon"><Lock /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <!-- 确认密码 -->
    <el-form-item prop="confirmPassword" label="确认密码" class="register-form-item">
      <el-input
        v-model="registerData.confirmPassword"
        type="password"
        placeholder="请再次输入密码"
        show-password
        clearable
      >
        <template #prefix>
          <el-icon class="el-input__icon"><Lock /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <!-- 用户名 -->
    <el-form-item prop="username" label="用户名" class="register-form-item">
      <el-input v-model="registerData.username" type="text" placeholder="请输入用户名(选填)" clearable>
        <template #prefix>
          <el-icon class="el-input__icon"><User /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <!-- 邮箱 -->
    <el-form-item prop="email" label="邮箱" class="register-form-item">
      <el-input v-model="registerData.email" type="email" placeholder="请输入邮箱地址(选填)" clearable>
        <template #prefix>
          <el-icon class="el-input__icon"><Message /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <!-- 用户类型选择 -->
    <el-form-item prop="userType" label="用户类型" class="register-form-item">
      <div class="user-type-section">
        <div class="user-type-grid">
          <div
            v-for="type in userTypes"
            :key="type.value"
            class="user-type-item"
            :class="{ selected: registerData.userType === type.value }"
            @click="selectUserType(type.value)"
          >
            {{ type.label }}
          </div>
        </div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { Phone, User, Message, Lock } from '@element-plus/icons-vue'

// 定义 props
const props = defineProps<{
  registerData: {
    username: string
    password: string
    email: string
    phone: string
    confirmPassword: string
    userType: string
  }
}>()

// 定义 emits
const emit = defineEmits<{
  'update:registerData': [value: any]
  'selectUserType': [type: string]
  'validate': [isValid: boolean]
}>()

// 表单引用
const registerFormRef = ref()

// 手机号正则
const PhoneReg = /^1[3-9]\d{9}$/

// 用户类型选项
const userTypes = [
  { value: 'Grower', label: '种植户', desc: '提供各类种植户' },
  { value: 'Plantation', label: '种植基地', desc: '提供种植基地' },
  { value: 'Trader', label: '贸易商', desc: '提供贸易服务' },
  { value: 'Factory', label: '工厂', desc: '提供产品加工' },
  { value: 'Company', label: '公司', desc: '企业用户' },
  { value: 'Individual', label: '个人', desc: '个人用户' },
]

// 注册表单校验规则
const registerRules = reactive({
  phone: [
    { required: true, message: '请填写手机号', trigger: 'blur' },
    { pattern: PhoneReg, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  username: [
    { required: false, message: '请填写用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: false, message: '请填写邮箱', trigger: 'blur' },
    { type: 'email' as const, message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请填写密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule: any, value: string, callback: Function) => {
        if (value !== props.registerData.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  userType: [
    { required: true, message: '请选择用户类型', trigger: 'change' }
  ]
})

// 选择用户类型
const selectUserType = (type: string) => {
  emit('selectUserType', type)
}

// 暴露校验方法给父组件
const validate = async () => {
  if (!registerFormRef.value) return false
  try {
    await registerFormRef.value.validate()
    emit('validate', true)
    return true
  } catch {
    emit('validate', false)
    return false
  }
}

// 暴露方法给父组件
defineExpose({
  validate
})
</script>

<style scoped lang="scss">
.register-form {
  .register-form-item {
    margin-bottom: 15px;

    .el-form-item__label {
      font-size: 14px;
      color: #333;
      font-weight: 500;
    }

    .el-form-item__content {
      display: block;
    }

    .el-form-item__error {
      color: #ff4444;
      font-size: 12px;
      padding-top: 4px;
      position: static;
      margin-top: 4px;
    }

    .el-input {
      width: 100%;
      max-width: 450px;

      .el-input__wrapper {
        height: 48px;
        padding: 0 15px;
      }

      .el-input__inner {
        height: 48px;
        line-height: 48px;
        font-size: 16px;
      }
    }
  }
}

.user-type-section {
  width: 100%;
  max-width: 450px;

  .user-type-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
    margin-top: 10px;

    .user-type-item {
      flex: 0 0 calc(33.333% - 5.33px);
      padding: 8px 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
      text-align: center;
      cursor: pointer;
      font-size: 14px;
      transition: all 0.3s;
      min-width: 0;
      position: relative;
      height: 36px;
      display: flex;
      align-items: center;
      justify-content: center;

      &:hover {
        border-color: #ff4444;
        background-color: #fff5f5;
      }

      &.selected {
        border-color: #ff4444;
        background-color: #ff4444;
        color: #fff;
        z-index: 1;
      }
    }
  }

  // 适配移动端
  @media (max-width: 480px) {
    .user-type-grid {
      .user-type-item {
        flex: 0 0 calc(50% - 4px);
        height: 36px;
      }
    }
  }
}
</style>
